package com.zyy.sbootai.controller;
import com.zyy.sbootai.domain.User;
import com.zyy.sbootai.service.UserService;
import com.zyy.sbootai.vo.UserVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController //声明类UserController是一个控制器类，接收前端发来的数据
@RequestMapping("/user")
public class UserController {
    /*让UserController‘认识’UserService*/
    @Resource
    private UserService userService;
    @RequestMapping("/hw")
    public String test() {
        return "Hello World!";
    }
    @RequestMapping("/login")
    public UserVO login(String username, String password) throws Exception {
        // 服务端对收到的用户名进行验证，不能为null，不能为空串
        if (username.equals(null) || username.equals("")) {
            throw new Exception("请输入用户名");
        }
        // 服务端对收到的密码进行验证，不能为null，不能为空串
        if (password.equals(null) || password.equals("")) {
            throw new Exception("密码用户名");
        }
        // 用户名和密码数据是合法的，将数据传给service
        User user = userService.login(username, password);
        if (user == null) {
            throw new Exception("用户名和密码错误");
        }
        UserVO userVO = new UserVO();
        userVO.setId(user.getId());
        userVO.setUserName(user.getUserName());
        userVO.setRealName(user.getRealName());
        userVO.setEmail(user.getEmail());
        return userVO;
    }
}